#include <sys/types.h>         
#include <stdio.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>

#define BACKLOG 10

int socket_connect(int *socket_fd,int *port)
{
	struct sockaddr_in 	serv_addr;

	*socket_fd=socket(AF_INET,SOCK_STREAM,0);
	int opt = 1;
	setsockopt(*socket_fd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(int));
	if(socket_fd<0)
	{
		printf("'socket' failure:%s\n",strerror(errno));
		return -1;
	}

	memset(&serv_addr,0,sizeof(serv_addr));
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(*port);
	serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);

	if(bind(*socket_fd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
	{
		printf("'bind' failure:%s\n",strerror(errno));
		close(*socket_fd);
		return -1;
	}

	listen(*socket_fd,BACKLOG);

}
